package com.ccjq.shosestore.member.dao.impl;

import com.ccjq.shosestore.core.dbmanager.DbConnection;
import com.ccjq.shosestore.member.dao.UserDao;
import com.ccjq.shosestore.member.po.UserModel;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoImpl implements UserDao {
    Connection conn = null;
    PreparedStatement st = null;


    public int save(UserModel model) {
        String sql = "insert into t_user values(?,?,?,?)";
        int result = 0;
        conn = DbConnection.getConnextion();
        try {
            st = conn.prepareStatement(sql);
            st.setString(1, model.getUserid());
            st.setString(2, model.getUsername());
            st.setString(3, model.getPassword());
            st.setString(4, model.getMobilePhone());
            result = st.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbConnection.close(null, st, conn);
        }
        return result;
    }

    public int update(UserModel model) {
        ResultSet re = null;
        String sql = "update t_user set username=?,password=?,mobile_phone=? where user_id=?";
        int result = 0;
        conn = DbConnection.getConnextion();
        try {
            st = conn.prepareStatement(sql);
            st.setString(1, model.getUsername());
            st.setString(2, model.getPassword());
            st.setString(3, model.getMobilePhone());
            st.setString(4, model.getUserid());
            result = st.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbConnection.close(null, st, conn);
        }
        return result;
    }

    public int delete(String userId) {
        ResultSet re = null;
        String sql = "delete from t_user where id=?";
        int result = 0;
        conn = DbConnection.getConnextion();
        try {
            st = conn.prepareStatement(sql);
            st.setString(1, userId);
            result = st.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbConnection.close(null, st, conn);
        }
        return result;
    }

    public UserModel findUserByName(String username) {
        ResultSet re = null;
        UserModel userModel = null;
        String sql = "select user_id,username,password,mobile_phone from t_user where username=?";
        conn = DbConnection.getConnextion();
        try {
            st = conn.prepareStatement(sql);
            st.setString(1, username);
            re = st.executeQuery();
            if (re.next()) {
                userModel = new UserModel();
                userModel.setUserid(re.getString("user_id"));
                userModel.setUsername(re.getString("username"));
                userModel.setPassword(re.getString("password"));
                userModel.setMobilePhone(re.getString("mobile_phone"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbConnection.close(re, st, conn);
        }
        return userModel;
    }

    public UserModel findUserByNamePw(String username, String password) {
        ResultSet re = null;
        UserModel userModel = null;
        String sql = "select user_id,username,password,mobile_phone from t_user where username=? and password=?";
        conn = DbConnection.getConnextion();
        try {
            st = conn.prepareStatement(sql);
            st.setString(1, username);
            st.setString(2, password);
            re = st.executeQuery();
            if (re.next()) {
                userModel = new UserModel();
                userModel.setUserid(re.getString("user_id"));
                userModel.setUsername(re.getString("username"));
                userModel.setPassword(re.getString("password"));
                userModel.setMobilePhone(re.getString("mobile_phone"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbConnection.close(re, st, conn);
        }
        return userModel;
    }

    public UserModel findUserById(String userId) {
        ResultSet re = null;
        UserModel userModel = null;
        String sql = "select user_id,username,password,mobile_phone from t_user where USER_ID=?";
        conn = DbConnection.getConnextion();
        try {
            st = conn.prepareStatement(sql);
            st.setString(1, userId);
            re = st.executeQuery();
            if (re.next()) {
                userModel = new UserModel();
                userModel.setUserid(re.getString("user_id"));
                userModel.setUsername(re.getString("username"));
                userModel.setPassword(re.getString("password"));
                userModel.setMobilePhone(re.getString("mobile_phone"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbConnection.close(re, st, conn);
        }
        return userModel;
    }

}
